﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class sqlFields : System.Web.UI.Page
{
    public string txtCont = "";
    public  string dbName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        dbName = Request.QueryString["dbname"];
        bindData();

    }

    private void bindData()
    {
        iData.mysqlDataAcess worker = new iData.mysqlDataAcess();
        string sql = @"select TABLE_COMMENT as tbltitle,TABLE_NAME as tblname from information_schema.TABLES
where table_schema='chromosome_db'";
        if (dbName != "")
        {
            sql = sql.Replace("chromosome_db", dbName);
        }
        else
        {
            sql = sql.Replace("chromosome_db", worker.dbSetting.database);

        }

        var dset = worker.Execute(sql);

        rptTblList.DataSource = dset;
        rptTblList.DataBind();

        rptTblAllList.DataSource = dset;
        rptTblAllList.DataBind();

        txtResult.Text = txtCont;
    }

    private System.Data.DataSet getTableFields(string tblName,string tbltitle)
    {
        iData.mysqlDataAcess worker = new iData.mysqlDataAcess();
        string sql = @"select TABLE_NAME as tblname,COLUMN_NAME as colname,COLUMN_COMMENT as memo,IS_NULLABLE as isnull,COLUMN_TYPE as typename from information_schema.COLUMNS
where table_schema='chromosome_db' and TABLE_NAME='taskdetail';

";
        if (dbName != "")
        {
            sql = sql.Replace("taskdetail", tblName).Replace("chromosome_db", dbName);
        }
        else
        {
            sql = sql.Replace("taskdetail", tblName).Replace("chromosome_db", worker.dbSetting.database);

        }
        var dset = worker.Execute(sql);
        var sb = new System.Text.StringBuilder();
        sb.Append("----------------------------\r\n");
        sb.AppendFormat("{0},{1},test,2\r\n", tbltitle, tblName);
        string sFldnames = "", sFldTitles = "", sFldTypes = "";
        for(int i = 0; i < dset.Tables[0].Rows.Count; i++)
        {
            var row = dset.Tables[0].Rows[i];
            if (i > 0)
            {
                sFldnames += ","; sFldTitles += ","; sFldTypes += ",";
            }
            string memo = row["memo"].ToString();
            if(memo=="")memo= row["colname"].ToString();
            memo=memo.Split(new char[] { ',', ':', '|', '，', '：' })[0];
            sFldnames += row["colname"].ToString();
            sFldTitles += memo;
            sFldTypes += row["typename"].ToString();
        }
        sb.Append(sFldTitles + "\r\n" + sFldnames + "\r\n" + sFldTypes + "\r\n\r\n");
        txtCont += sb.ToString();
        return dset;


    }

    protected void rptTblAllList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void rptTblAllList_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rep = e.Item.FindControl("rptFieldList") as Repeater;//找到里层的repeater对象
            System.Data.DataRowView rowv = (System.Data.DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项 
            string tblName = rowv["tblname"].ToString();
            string tbltitle = rowv["tbltitle"].ToString();
            rep.DataSource = getTableFields(tblName, tbltitle);
            rep.DataBind();
        }
    }

}